﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Data;
using System.Configuration;
using System.Diagnostics;
using Microsoft.Win32;


namespace PGI.AD
{
    internal class DataBaseFactory
    {

        #region Variables Privadas
        private static SqlConnection m_SqlBasedeDatos;
        #endregion

        #region Propiedades
        public static SqlConnection SqlBasedeDatos
        {
            get { return m_SqlBasedeDatos; }
            set { m_SqlBasedeDatos = value; }
        }
        #endregion

        #region SqlConexion
        public static SqlConnection SqlOpenDatabase(string _nomConexion)
        {
            try
            {
                if (SqlBasedeDatos == null)
                {

                    Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    ConnectionStringsSection configSection = configuration.GetSection("connectionStrings") as ConnectionStringsSection;
                    if ((!(configSection.ElementInformation.IsLocked)) && (!(configSection.SectionInformation.IsLocked)))
                    {
                        if (configSection.SectionInformation.IsProtected)
                        {
                            configSection.SectionInformation.UnprotectSection();
                        }                        
                    }
                    
                    string conn = ConfigurationManager.ConnectionStrings[_nomConexion].ConnectionString;
                    SqlBasedeDatos = new SqlConnection(conn);

                    if (!configSection.SectionInformation.IsProtected)
                    {
                        configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
                    }
                }

                if (SqlBasedeDatos.State == System.Data.ConnectionState.Closed)
                {
                    SqlBasedeDatos.Open();
                }
            }
            catch { SqlBasedeDatos = null; }

            return SqlBasedeDatos;
        }

        public static void SqlCloseDatabase(string _nomConexion)
        {
            try
            {
                if (SqlBasedeDatos == null)
                {
                    string conn = ConfigurationManager.ConnectionStrings[_nomConexion].ConnectionString;
                    //Leer de registro
                    //RegistryKey key = Registry.LocalMachine.OpenSubKey("HKEY_LOCAL_MACHINE\\SOFTWARE\\TICSys\\SI", true);
                    //conn = Convert.ToString(key.GetValue("conexion_2"));
                    SqlBasedeDatos = new SqlConnection(conn);
                }
                if (SqlBasedeDatos.State == System.Data.ConnectionState.Open)
                {
                    SqlBasedeDatos.Close();
                    SqlBasedeDatos = null;
                }
            }
            catch { SqlBasedeDatos = null; }
            finally { SqlBasedeDatos = null; }
        }

        #endregion

    }
}
